package com.yueke.gemini.modules.jwt.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.yueke.gemini.modules.jwt.utils.JwtUtil;
import com.yueke.gemini.utils.R;

/**
 * jwt
 * @author qxw
 * @data 2018年8月6日上午11:27:44
 */

@Controller
@RequestMapping("/jwt")
public class JwtController {
	
	/**
	 * 需要token才可以请求
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/api/hellWorld")
	public R hellWorld(@RequestHeader(value=JwtUtil.USER_NAME)String userId){
		return R.ok().put("userId", userId);
	}
	
	/**
	 * 登录获取token
	 * @param username
	 * @param name
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/login")
	public R login(@RequestParam(value="username",required=true) String username,
			@RequestParam(value="password",required=true) String password ){
		 try {
			 if(username.equals("admin")&&password.equals("admin")){
				 String jwt = JwtUtil.generateToken(username);
				 return R.ok().put("token", jwt);
			 }else{
				return R.error("账号密码错误"); 
			 }
		} catch (Exception e) {
			e.printStackTrace();
			return R.error();
		}
	}
	
	
}
